﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.Statistics;
using MINERP.MODEL.FoundationModel;
using MINERP.COM;
using MINERP.BLL;
using MINERP.MODEL.Statistics;
using DevExpress.XtraEditors.Controls;
using System.Threading;
using MINERP.ComControls.Extension;

namespace MINERP.Statistics.Forms.ProfessionalReports
{
    public partial class FrmPerformance3 : Form
    {
        private static FrmPerformance3 _instance;
        public static FrmPerformance3 GetInstance
        {
            get
            {
                if (_instance == null || _instance.IsDisposed)
                {
                    _instance = new FrmPerformance3();
                }
                return _instance;
            }
        }
        public FrmPerformance3()
        {
            InitializeComponent();
        }

        void BandingGroup(List<Tb_WorkerGroup_Info> list)
        {
            cmbGroup.DisplayMember = "GroupName";
            cmbGroup.ValueMember = "GroupId";
            cmbGroup.DataSource = list;
        }
        PerforMoneyBll _bll = new PerforMoneyBll();
        
        private void FrmPerformance_Load(object sender, EventArgs e)
        {
            progressBar1.Minimum = 0;
            dtStart.DateTime = DateTime.Now;
            dtEnd.DateTime = DateTime.Now;
            _bll.ShowCurrPointHandle+=new PerforMoneyBll.ShowProcess(ShowCurrPoint);
            _bll.ShowMsgHandle+=new PerforMoneyBll.ShowMessage(ShowMsg);
            _bll.ShowCountHandle += new PerforMoneyBll.ShowProcess(ShowCount);
            BandingGroup(_bll.GetAllGroup());
        }
        delegate void DisPalyProgress(int intCurr);
        void ShowCurrPoint(int inCurr)
        {
            if (!this.InvokeRequired)
            {
               
                progressBar1.Value = inCurr;
            }
            else
            {
                this.BeginInvoke(new DisPalyProgress(ShowCurrPoint), new object[] { inCurr });
            }
           
        }
        delegate void DisPalyLable(string str);
        void ShowMsg(string inStr)
        {
            if (!this.InvokeRequired)
            {
                labelDisplay.Text = inStr;
            }
            else
            {
                this.BeginInvoke(new DisPalyLable(ShowMsg), new object[] { inStr });
            }
            
        }

        delegate void DisPalyCount(int count);
        void ShowCount(int inCurr)
        {
            if (!this.InvokeRequired)
            {

                progressBar1.Maximum = inCurr;
            }
            else
            {
                this.BeginInvoke(new DisPalyCount(ShowCount), new object[] { inCurr });
            }

        }
        private void cmbGroup_SelectionChangeCommitted(object sender, EventArgs e)
        {
            int GroupId = ((Tb_WorkerGroup_Info)this.cmbGroup.SelectedItem).GroupId;
            BindWorkerList(GroupId);
        }
        private void BindWorkerList(int GroupId)
        {
            List<UserInfo> userList = new List<UserInfo>();
            if (GroupId > 0)
            {
                userList = _bll.GetUserListByGroupId(GroupId);
            }
            cmkUser.Properties.DataSource = userList;
            cmkUser.Properties.DisplayMember = "UserName";
            cmkUser.Properties.ValueMember = "UserID";
        }
        bool _isRun = false;
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (_isRun == true)
            {
                MessageBox.Show("正在统计，请稍后");
                return;
            }
            _isRun = true;
            this.Cursor = Cursors.WaitCursor;
            if (dtStart.DateTime > dtEnd.DateTime)
            {
                MessageBox.Show("起始日期必须小于结束日期");
                dtStart.DateTime = dtEnd.DateTime.Date;
                this.Cursor = Cursors.Default;
                return;
            }

           
            Thread thMake = new Thread(ExcOper);
            thMake.IsBackground = true;
            thMake.Start();
            
            this.Cursor = Cursors.Default;
            
        }
        void ExcOper()
        {
            List<int> selectedUserIDList = new List<int>();
            foreach (CheckedListBoxItem item in cmkUser.Properties.Items)
            {
                if (item.CheckState == CheckState.Checked)
                {
                    selectedUserIDList.Add((int)item.Value);
                }
            }

            if (selectedUserIDList.Count < 1)
            {
                MessageBox.Show("请选择要查询的员工!");
                //this.Cursor = Cursors.Default;
                return;
            }
            if (!checkBoxBackUp.Checked)
            {
                List<PerforDeatailInfo> listPM = _bll.GetStatisticPerforDetail(dtStart.DateTime, dtEnd.DateTime, selectedUserIDList);
                DisplayView(listPM);
            }
            else
            {
                List<PerforDeatailInfo> listPM = _bll.GetStatisticPerforDetailBK(dtStart.DateTime, dtEnd.DateTime, selectedUserIDList);
                DisplayView(listPM);
            }
            _isRun = false;
        }
        delegate void deleteDisplayView(List<PerforDeatailInfo> listPM);

        void DisplayView(List<PerforDeatailInfo> listPM)
        {
            if (!this.InvokeRequired)
            {
                gcEmployeePerformance.DataSource = listPM;
                gcEmployeePerformance.RefreshDataSource();
                labelDisplay.Text = "请将计算结果导出并保存";
            }
            else
            {
                this.BeginInvoke(new deleteDisplayView(DisplayView), new object[] { listPM });
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            //{
            //    //saveFileDialog1.FileName = cmbGroup.Text+"组的员工业绩.xls";

            //    string saveDir = saveFileDialog1.FileName;
            //    if (!string.IsNullOrEmpty(saveDir))
            //    {
            //        gridView1.ExportToXls(saveDir);
            //        MessageBox.Show("导出成功!");
            //    }
            //}
            //DevGridViewExport.ExportToExcel("人员业绩" + DateTime.Now.ToString("yyyyMMdd HHmmss"), (DevExpress.XtraGrid.Views.Grid.GridView)gcEmployeePerformance.MainView);
            DevControlExtension.ExportToExcel(gcEmployeePerformance,this,"人员业绩" + DateTime.Now.ToString("yyyyMMdd HHmmss"));
        }
    }
}
